/** @odoo-module **/
import {dinnerTypeNameComparator,dishNumIdNameComparator} from './comparator.js'
import init_grid_options from './gridOptions.js'

const col = (owl) => {
    let col = [
    {
        headerName: '餐别',
        field: 'dinner_type_category_name',
        width: 80,
        editable: false,
        menuTabs: [],
        sortable: true,
        // showRowGroup:true,
        rowGroup: true,
        hide:true,
        // comparator: dinnerTypeNameComparator
    },
    {
        headerName: '餐别',
        field: 'dinner_type_name',
        width: 80,
        editable: false,
        menuTabs: [],
        sortable: true,
        // showRowGroup:true,
        // rowGroup: true,
        hide:true,
        // comparator: dinnerTypeNameComparator
    },
    // {
    //     headerName: '类别',
    //     field: 'dish_top_category_name_cn',
    //     width: 50,
    //     editable: false,
    //     sortable: true,
    //     menuTabs: []
    // },
    {
        headerName: '序号',
        field: 'dish_num_id_name',
        width: 50,
        editable: false,
        menuTabs: [],
        comparator: dishNumIdNameComparator
    },
    {
        headerName: 'dish_num_id',
        field: 'dish_num_id',
        hide:true
    },
    {
        headerName: 'dish_top_category_id',
        field: 'dish_top_category_id',
        hide:true,
        sortable: true,
        menuTabs: []
    },
    {
        headerName: 'LevelScore',
        field: 'LevelScore',
        width: 80,
        editable: false,
        hide:true,
        sortable: true,
        menuTabs: []
    }];
    for (let i = 1; i < 8; i++) {
        col.push({
            headerName: '周'+i,
            field: 'day' + i + '_dish_key_name',
            flex:1,
            editable:false,
            menuTabs: [],
            // dndSource: true,
            // dndSourceOnRowDrag: onRowDrag,
            cellRenderer:DragSourceRenderer
        });
    }
    return col;
};

class DragSourceRenderer {
    init(params) {
        let eTemp = document.createElement('div');
        // if (params.value == "" || params.value == undefined) return "";
        eTemp.innerHTML = '<div style="cursor: grab;" draggable="true">'+params.value+'</div>';

        this.eGui = eTemp.firstChild;
        this.rowNode = params.node;
        this.value = params.value;
        this.day = params.colDef.field.split('_')[0];
        this.data = params.data;
        this.dish_key = params.data[this.day+'_dish_key'];

        this.onDragStartListener = this.onDragStart.bind(this);
        this.eGui.addEventListener('dragstart', this.onDragStartListener);

        this.owl = params.context.owl_widget;
    }

    onDragStart(dragEvent) {
        // var userAgent = window.navigator.userAgent;
        let _this = this;
        let jsonObject = _this.dish_key;
        let jsonData = JSON.stringify(jsonObject);
        // console.log(jsonData);
        dragEvent.dataTransfer.setData('application/json', jsonData);
        dragEvent.dataTransfer.setData('text/plain', jsonData);
        this.owl.dragingData = jsonObject;
    }

    getGui() {
        return this.eGui;
    }

    refresh(params) {
        return false;
    }

    destroy() {
        this.eGui.removeEventListener('dragstart', this.onDragStartListener);
    }
}

const grid_options = (owl) => {
    const gridOptions = {
        columnDefs:col(owl),
        groupRowRendererParams: {
            suppressCount: true,
            innerRenderer: (params)=>{
                return `<span class="" style="font-family:'黑体'">${params.value}</span>`
            },
            editable:false
        },
        enableRangeSelection:false,
        suppressRowClickSelection: true,
        rowHeight:28,
        headerHeight:22,
        context: {
            owl_widget: this
        },
        getContextMenuItems: (e) => {
            return [];
        },
        getRowId: (params) => {
            return params.data.id;
        },
        rowDragManaged: true,
        onGridReady: function (params) {
            let defaultSortModel = [
                // { colId: 'dinner_type_category_name', sort: 'asc', sortIndex: 0 },
                { colId: 'LevelScore', sort: 'asc', sortIndex: 0 },
                // { colId: 'dish_top_category_id', sort: 'asc', sortIndex: 1 },
            ];
            params.columnApi.applyColumnState({ state: defaultSortModel });
        },
    };

    return Object.assign({},init_grid_options(),gridOptions);
}


export default grid_options
